/*
 *   A task scheduler that implements Monte-Carlo simulation
 *
 *   Copyright (C) 2011 Systasis Computer Systems, Inc.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   $Id: StoragePresenter.java 91 2011-08-10 12:28:23Z jchimene@gmail.com $
 */
package com.systasis.sked.client.presenter;

import java.util.logging.Logger;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.systasis.sked.client.event.RestoreEvent;
import com.systasis.sked.client.event.RestoreEventHandler;
import com.systasis.sked.client.event.SaveEvent;
import com.systasis.sked.client.event.SaveEventHandler;
import com.systasis.sked.client.model.SkedStorage;

@Singleton
public class StoragePresenter implements StorageHandlers {

    @SuppressWarnings("unused")
    final private static Logger logger = Logger.getLogger(StoragePresenter.class.getName());

    final private ActivityPresenter activityPresenter;

    final private ActivityOnNodeScheduler scheduler;

    final private SkedStorage skedStorage = new SkedStorage();

    @Inject
    public StoragePresenter(EventBus eventBus //
	    , final ActivityPresenter activityPresenter //
	    , final ActivityOnNodeScheduler scheduler) {
	this.scheduler = scheduler;
	this.activityPresenter = activityPresenter;
	eventBus.addHandler(SaveEvent.getType(), new SaveEventHandler() {

	    @Override
	    public void onSaveEvent(SaveEvent event) {
		onSave();

	    }
	});

	eventBus.addHandler(RestoreEvent.getType(), new RestoreEventHandler() {

	    @Override
	    public void onRestoreEvent(RestoreEvent event) {
		onRestore();

	    }
	});
    }

    public void onRestore() {
	try {
	    for (final String r0 : skedStorage.getStorageMap().values()) {
		skedStorage.open(r0);
		logger.info("import: " + r0 + "\n" + skedStorage.toString());
		activityPresenter.onActivityChange(Activity.importerStorage(skedStorage.get()));
	    }

	    activityPresenter.onActivityRestore();
	} catch (final Exception e) {
	    e.printStackTrace();
	}
    }

    @Override
    public void onRestoreEvent(RestoreEvent event) {
	onRestore();
    }

    public void onSave() {
	try {
	    skedStorage.getStorageMap().clear();
	} catch (final Exception e1) {
	    e1.printStackTrace();
	}

	for (final Activity activity : scheduler.activityIterable()) {
	    activity.exporterStorage(skedStorage.get());
	    try {
		skedStorage.getStorageMap().put(activity.getActivityName(), skedStorage.toString());
	    } catch (final Exception e) {
		e.printStackTrace();
	    }
//	    logger.info(skedStorage.toString());
	}
    }

    @Override
    public void onSaveEvent(SaveEvent event) {
	onSave();
    }
}
